package cc.yugu.yhdw.webserver.service.hdw;

import cc.yugu.yhdw.common.domain.hdw.ExchangeAmountMoneyDivideDetail;
import cc.yugu.yhdw.common.entity.json.ExtResult;
import cc.yugu.yhdw.common.entity.pagination.Page;
import cc.yugu.yhdw.webserver.persistence.basic.AgentMapper;
import cc.yugu.yhdw.webserver.persistence.hdw.AgencyMapper;
import cc.yugu.yhdw.webserver.persistence.hdw.ExchangeAmountMoneyDivideDetailMapper;
import cc.yugu.yhdw.webserver.persistence.hdw.SupplierMapper;
import cc.yugu.yhdw.webserver.service.AbstractService;
import cc.yugu.yhdw.webserver.service.basic.AgentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * 换电次数金额划分明细业务
 * 
 * @Date:Created in 2018/7/9 9:56
 * @Author:
 * @Modified By: zjn 2018/7/9 9:56
 */ 
@Service
public class ExchangeAmountMoneyDivideDetailService extends AbstractService {
    @Autowired
    ExchangeAmountMoneyDivideDetailMapper exchangeAmountMoneyDivideDetailMapper;
    @Autowired
    AgentMapper agentMapper;
    @Autowired
    AgencyMapper agencyMapper;
    @Autowired
    SupplierMapper supplierMapper;
    /**
    * 分页查询
    * 
    * @Param: [exchangeAmountMoneyDivideDetail]
    * @Return: cc.yugu.yhdw.common.entity.pagination.Page
    * @Author By: zjn 2018/7/9  9:56
    */ 
    public Page findPage(ExchangeAmountMoneyDivideDetail exchangeAmountMoneyDivideDetail) {
        Page page = exchangeAmountMoneyDivideDetail.buildPage();
        page.setTotalItems(exchangeAmountMoneyDivideDetailMapper.findPageCount(exchangeAmountMoneyDivideDetail));
        exchangeAmountMoneyDivideDetail.setBeginIndex(page.getOffset());

        List<ExchangeAmountMoneyDivideDetail> exchangeAmountMoneyDivideDetailList = exchangeAmountMoneyDivideDetailMapper.findPageResult(exchangeAmountMoneyDivideDetail);
        page.setResult(exchangeAmountMoneyDivideDetailList);

        ExchangeAmountMoneyDivideDetail.OrgType[] types = ExchangeAmountMoneyDivideDetail.OrgType.values();
        for (ExchangeAmountMoneyDivideDetail e : exchangeAmountMoneyDivideDetailList) {
            for (int i = 0; i < types.length; i++) {
                if (e.getOrgType() == types[i].getValue()) {
                    e.setOrgTypeName(types[i].getName());
                }
            }
            if (e.getOrgType() == ExchangeAmountMoneyDivideDetail.OrgType.HQ.getValue()) {
                e.setOrgIdName("总部");
            } else if (e.getOrgType() == ExchangeAmountMoneyDivideDetail.OrgType.AGENT.getValue()) {
                e.setOrgIdName(agentMapper.find(e.getOrgId()).getAgentName());
            } else if (e.getOrgType() == ExchangeAmountMoneyDivideDetail.OrgType.AGENCY.getValue()) {
                e.setOrgIdName(agencyMapper.find(e.getOrgId()).getAgencyName());
            } else {
                e.setOrgIdName(supplierMapper.find(e.getOrgId()).getSupplierName());
            }
        }

        return page;
    }
    /**
    * 根据主键查询表hdw_exchange_amount_money_divide_detail
    * 
    * @Param: [id]
    * @Return: cc.yugu.yhdw.common.domain.hdw.ExchangeAmountMoneyDivideDetail
    * @Author By: zjn 2018/7/9  9:56
    */ 
    public ExchangeAmountMoneyDivideDetail find(int id) {
        return exchangeAmountMoneyDivideDetailMapper.find(id);
    }
    /**
    * 对金额分配比例求和
    * 
    * @Param: [exchangeAmountMoneyDivideDetail]
    * @Return: int
    * @Author By: zjn 2018/7/9  10:01
    */ 
    public int unique(ExchangeAmountMoneyDivideDetail exchangeAmountMoneyDivideDetail) {
        return exchangeAmountMoneyDivideDetailMapper.unique(exchangeAmountMoneyDivideDetail);
    }
    /**
    * 插入
    * 
    * @Param: [exchangeAmountMoneyDivideDetail]
    * @Return: cc.yugu.yhdw.common.entity.json.ExtResult
    * @Author By: zjn 2018/7/9  10:01
    */ 
    public ExtResult insert(ExchangeAmountMoneyDivideDetail exchangeAmountMoneyDivideDetail) {
        if (exchangeAmountMoneyDivideDetailMapper.checkRepeat(exchangeAmountMoneyDivideDetail) > 0) {
            return ExtResult.failResult("数据已存在不能重复添加");
        }
        if (exchangeAmountMoneyDivideDetail.getPercent() < 0) {
            exchangeAmountMoneyDivideDetail.setPercent(0);
        }
        int total = exchangeAmountMoneyDivideDetailMapper.insert(exchangeAmountMoneyDivideDetail);
        if (total == 0) {
            return ExtResult.failResult("新增失败");
        }
        return ExtResult.successResult();
    }
    /**
    * 按主键更新表
    * 
    * @Param: [exchangeAmountMoneyDivideDetail]
    * @Return: cc.yugu.yhdw.common.entity.json.ExtResult
    * @Author By: zjn 2018/7/9  10:02
    */ 
    public ExtResult update(ExchangeAmountMoneyDivideDetail exchangeAmountMoneyDivideDetail) {
        if (!exchangeAmountMoneyDivideDetail.equals(exchangeAmountMoneyDivideDetailMapper.find(exchangeAmountMoneyDivideDetail.getId())) && exchangeAmountMoneyDivideDetailMapper.checkRepeat(exchangeAmountMoneyDivideDetail) > 0) {
            return ExtResult.failResult("数据已存在不能重复添加");
        }
        if (exchangeAmountMoneyDivideDetail.getPercent() < 0) {
            exchangeAmountMoneyDivideDetail.setPercent(0);
        }
        int total = exchangeAmountMoneyDivideDetailMapper.update(exchangeAmountMoneyDivideDetail);
        if (total == 0) {
            return ExtResult.failResult("修改失败");
        }
        return ExtResult.successResult();
    }
    /**
    * 删除
    * 
    * @Param: [id]
    * @Return: int
    * @Author By: zjn 2018/7/9  10:02
    */ 
    public int delete(int id) {
        return exchangeAmountMoneyDivideDetailMapper.delete(id);
    }

}
